﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

public partial class basement : System.Web.UI.Page
{
    ParkingFloor parkingFloor = new ParkingFloor();
    //Inject log4net
    private static readonly ILog logger = LogManager.GetLogger(typeof(basement).Name);
    protected void Page_Load(object sender, EventArgs e)
    {
        Inits();
        if (!Page.IsPostBack)
        {
            if (!string.IsNullOrEmpty(hdnId.Value))
                LoadUpdate(hdnId.Value);
        } 
    }

    #region Private methods
    /// <summary>
    /// Init page
    /// </summary>
    /// <param name="id">string</param>
    private void Inits()
    {
        lblErrorName.Visible = false;
        //handle [Xoa] button
        btnReset.ImageUrl = "images/xoa.gif";
        btnReset.Attributes.Add("onmouseover", "this.src='images/xoa_hover.gif'");
        btnReset.Attributes.Add("onmouseout", "this.src='images/xoa.gif'");

        //handle [Save] button
        btnSave.Attributes.Add("onmouseover", "this.src='images/save_hover.gif'");
        btnSave.Attributes.Add("onmouseout", "this.src='images/save.gif'");

        //page title
        Page.Title = Constants.TITLE_SYSTEM + Constants.SPACE + Constants.SEPARATE + Constants.SPACE + lblTitle.Text;
        LoadToGrid();
    }

    /// <summary>
    /// LoadUpdate
    /// </summary>
    /// <param name="id">Guid</param>
    private void LoadUpdate(string id)
    {
        //handle [Save] button
        btnSave.ImageUrl = "images/edit.jpg";
        btnSave.Attributes.Add("onmouseover", "this.src='images/edit_hover.jpg'");
        btnSave.Attributes.Add("onmouseout", "this.src='images/edit.jpg'");

        ParkingFloor parking = null;
        try
        {
            parking = parkingFloor.GetById(new Guid(id));
            //Load to control
            txtName.Text = parking.Name;
            txtDes.Text = parking.Description;
            hdnId.Value = id;
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }


    /// <summary>
    /// LoadToGrid
    /// </summary>
    private void LoadToGrid()
    {
        try
        {
            IList<ParkingFloor> list = parkingFloor.GetAll();
            if (list != null)
            {
                rptBasement.DataSource = list;
                rptBasement.DataBind();
                lblInform.Text = "";
                rptBasement.Visible = true;
            }
            else
            {
                lblInform.Text = "Không có hầm nào.";
                rptBasement.Visible = false;
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    /// <summary>
    /// InsertOrUpdate
    /// </summary>
    private void InsertOrUpdate()
    {
        if (String.IsNullOrEmpty(hdnId.Value))
        {
            if (IsExistCardType())
                return;
        }

        //get Employee from session
        Employee emp = Utils.GetEmployeeSession();


        //init BO
        ParkingFloor parking = new ParkingFloor();
        //set to BO  
        if (!String.IsNullOrEmpty(hdnId.Value))
            parking.ID = new Guid(hdnId.Value);
        parking.Name = txtName.Text.Trim();
        parking.Description = txtDes.Text.Trim();
        Employee em = new Employee();
        em.Id = emp.Id;
        parking.LastUser = em;
        try
        {
            if (parkingFloor.InsertOrUpdate(parking))
            {
                LoadToGrid();//load grid again 
                SetControlBlank();
                hdnId.Value = "";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }


    /// <summary>
    /// IsExistCardType
    /// </summary>
    private bool IsExistCardType()
    {
        bool flag = false;
        try
        {
            if (parkingFloor.IsExistFloor(txtName.Text.Trim()))
            {
                lblErrorName.Text = "Thẻ này đã tồn tại!";
                lblErrorName.Visible = true;
                flag = true;
            }
            else
            {
                lblErrorName.Text = "";
                lblErrorName.Visible = false;
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
        return flag;
    }

    /// <summary>
    /// Delete
    /// </summary>
    private void Delete(string id)
    {
        try
        {
            if (parkingFloor.Delete(new Guid(id)))
            {
                LoadToGrid();
                SetControlBlank();
                hdnId.Value = "";
            }
        }
        catch (Exception ex)
        {
            //lblMsg.Text = Constants.ERROR_MSG;
            lblMsg.Text = "Hầm này đã được sử dụng";
            lblErrorName.Visible = true;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    /// <summary>
    /// Action: press delete and edit
    /// </summary>
    /// <param name="strCommandName"></param>
    /// <param name="strArgument"></param>
    private void Action(string strCommandName, string strArgument)
    {
        switch (strCommandName.ToUpper())
        {
            //press delete button
            case "DELETE":
                Delete(strArgument);
                break;
            //press edit icon
            case "EDIT":
                LoadUpdate(strArgument);
                break;
            default:
                break;
        }
    }

    private void SetControlBlank()
    {
        txtName.Text = "";
        txtDes.Text = "";
    }

    #endregion

    #region Events
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {

        InsertOrUpdate();
    }
    protected void btnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtName.Text = "";
        txtDes.Text = "";
    }
    protected void rptBasement_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //get command
        string strCommandName = e.CommandName.Trim();
        string strArgument = e.CommandArgument.ToString().Trim();

        hdnId.Value = strArgument;
        Action(strCommandName, strArgument);
    }
    #endregion
}